********************************************************************************
** 	TITLE:		CA88_results                                                  ** 
**	AUTHOR:	    Philippe Mongrain                                             **
**	DATA:       clea_lc_20201216                                              **
**	VERSION:	Stata 16					                                  **
**	DATE:		October 2022  				                                  **
********************************************************************************

* Version control

version 16.0

* Open log file

capture log close
log using "CA88_results.smcl", replace

* Download the "clea_lc_20201216.dta" file from this link: http://www.electiondataarchive.org/clea-lower-chamber-elections-archive.php

* Open the dataset

use "clea_lc_20201216", clear

* Keep the results of the 1988 Canadian federal election

keep if ctr_n == "Canada"

keep if yr == 1988

* Generate party variable

gen party88 = 1 if pty_n == "liberal"
replace party88 = 2 if pty_n == "progressive conservative"
replace party88 = 3 if pty_n == "new democratic party"
replace party88 = 88 if party88 == .

* Generate rank of parties

gsort cst_n -cv1
bysort cst_n : gen rank88 = _n

gen first88 = party88 if rank88 == 1
gen second88 = party88 if rank88 == 2
gen third88 = party88 if rank88 == 3

bysort cst_n : gen winner88 = first88[1]
bysort cst_n : gen runnerup88 = second88[2]
bysort cst_n : gen thirdplace88 = third88[3]

* Generate vote shares

gen share88 = (cv1/vv1)*100

* Generate margins of victory

bysort cst_n : gen margin88 = share88[1] - share88[2]

* Generate effective number of electoral parties

gen sqrvotepr88 = (share88/100)^2

bysort cst_n : egen sumsqrvotepr88 = sum(sqrvotepr88)

gen enep88 = 1/sumsqrvotepr88

* Labelling parties

label define party 1 "Liberal Party" 2 "Progressive Conservative Party" 3 "NDP" 88 "Other"
label values winner88 runnerup88 thirdplace88 party

* Selecting, ordering, and sorting variables

keep cst cst_n party88 rank88 cv1 share88 margin88 enep88 winner88 runnerup88 thirdplace88

order cst cst_n party88 rank88 cv1 share88 margin88 enep88 winner88 runnerup88 thirdplace88

sort cst rank88

drop if rank88 > 3

* Drop duplicates

duplicates tag cst, gen(dup)
duplicates drop cst, force
drop dup

* Create election variable

gen election88 = "1988 Canadian federal election"

* Saving file

save CA88_results, replace